﻿using AntdUI;
using PosClient.Delegates;
using PosClient.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.Controls
{
    public partial class GoodGrid : Control, INotifyPropertyChanged
    {
        public GoodGrid()
        {
            InitializeComponent();
        }

        public event GridIndexEventHandler GridIndex;

        private int _Index;
        private BindingList<PromoteEventItem> _DataSource = new BindingList<PromoteEventItem>();

        public int Index
        {
            get => _Index;
            set
            {
                if (_Index == value) return;
                _Index = value;
                NotifyPropertyChanged();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [MergableProperty(false)]
        public BindingList<PromoteEventItem> DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                if (_DataSource == value) return;
                _DataSource = value;
                ReloadList();
                NotifyPropertyChanged(nameof(_DataSource));
            }
        }

        private void ReloadList()
        {
            flowPanel_base.Controls.Clear();
            for (int i = 0; i < _DataSource.Count; i++)
            {
                var pro = _DataSource[i];
                GoodRow row = new GoodRow();
                row.Title = $"{pro.ItemCode}-{pro.ItemName}";
                row.Price = $"￥换购价:{pro.ItemValue}";
                row.Svg = pro.MainPic;
                row.TagData = pro;
                row.RowClick += RowChoose;
                flowPanel_base.Controls.Add(row);
            }
            RowChoose(flowPanel_base.Controls[0],null);
        }

        public void GridKeyUp(Keys e)
        {
            if (e == Keys.Left)
            {
                if (_Index > 0 && _Index < flowPanel_base.Controls.Count)
                {
                    RowChoose(flowPanel_base.Controls[_Index - 1], null);
                }
            }
            else if (e == Keys.Right)
            {
                if (_Index < flowPanel_base.Controls.Count - 1)
                {
                    RowChoose(flowPanel_base.Controls[_Index + 1], null);
                }
            }
        }

        private void RowChoose(object sender, EventArgs e)
        {
            foreach (var control in flowPanel_base.Controls)
            {
                if (control is GoodRow)
                {
                    (control as GoodRow).Check = false;
                }
            }
            var activeControl = (sender as GoodRow);
            activeControl.Check = true;
            _Index = activeControl.Index;
            GridIndex?.Invoke(activeControl, e);//通知更新索引
            activeControl.Check = true;
            flowPanel_base.ScrollControlIntoView(activeControl);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); //实现通知刷新
        }
    }
}
